﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;

namespace ARR.DataModel.Repositories
{
    public static partial class ArticlesRepository
    {
        public static void UpdateFromTempArticle(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("Articles_UpdateFromTempArticles", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);

                database.ExecuteNonQuery();
            }
        }

        public static void UpdateStatus(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 900;
                database.NewCommand("Articles_UpdateStatus", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);

                database.ExecuteNonQuery();
            }
        }
        
        public static void UpdateArticlesStatus(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 900;
                database.NewCommand("Articles_UpdateArticleStatus", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);

                database.ExecuteNonQuery();
            }
        }

        public static void InsertFromTempSpaces(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("Articles_InsertFromTempSpaces", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);

                database.ExecuteNonQuery();
            }
        }

        public static void UpdateSpaces(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("Articles_UpdateSpaces", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);

                database.ExecuteNonQuery();
            }
        }

        public static void InsertFromTempArticles(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("Articles_InsertFromTempArticles", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);

                database.ExecuteNonQuery();
            }
        }

        public static void InsertFromTempInventories(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 900;
                database.NewCommand("Articles_InsertFromTempInventories", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);

                database.ExecuteNonQuery();
            }
        }

        public static int InsertFromTempArticlesAsDelisted(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 900;
                database.NewCommand("Articles_InsertFromTempArticlesAsDelisted", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);
                database.AddOutParameter("@TotalInsertedRows", DbType.Int32);
                database.ExecuteNonQuery();

                int totalRows = (int)database.GetParameterValue("@TotalInsertedRows");

                return totalRows;                
            }
        }
        
        public static void Join(int[] articleIds, int targetArticleId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("Articles_Join", CommandType.StoredProcedure);
                database.AddInParameter("@ArticleIds", DbType.String, String.Join(",", articleIds));
                database.AddInParameter("@TargetArticleId", DbType.Int32, targetArticleId);

                database.ExecuteNonQuery();
            }
        }

        public static void Split(int sourceArticleId, int[] articleIds)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("Articles_Split", CommandType.StoredProcedure);

                database.AddInParameter("@SourceArticleId", DbType.Int32, sourceArticleId);
                database.AddInParameter("@ArticleIds", DbType.String, String.Join(",", articleIds));

                database.ExecuteNonQuery();
            }
        }
    }
}
